package com.lance.answer.gui.answer.gk.answer.lms.batch;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.gk.answer.lms.batch.dto.LmsWatchDTO;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.GetCourseUrlTask;
import com.lance.answer.gui.answer.gk.answer.lms.batch.task.GetUserCourseInfoTask;
import com.lance.answer.gui.answer.gk.answer.lms.util.LmsUtil;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * 获取每个人的课程信息
 */
public class GetCourseUrlInfo {

    public static void main(String[] args) throws Exception {

       // Thread.sleep(1000*60*60*5);
        int sync = 15;

        LinkedBlockingDeque<LmsWatchDTO> watchQueue = new LinkedBlockingDeque<>();

     //   InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//李俊杰2.user");
     //   InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//all3.user");
        InputStream inputStream2 = new FileInputStream("libs//lmsTeachers//刘俊伟.user");
        List<String> userString = IOUtils.readLines(inputStream2, "utf-8");

        for (String user : userString) {
            String[] strs = user.split(",");
            String userCode = strs[0];
            String pwd = strs[1];


            LmsWatchDTO lmsWatchDTO = new LmsWatchDTO();
            lmsWatchDTO.setUsername(userCode);
            lmsWatchDTO.setPassword(pwd);

            Map<Long, String> courseMap = LmsUtil.getUserCourseMap(userCode);
            Map<Long, String> newCourseMap = Maps.newHashMap();
            if(null == courseMap){
                continue;
            }

            for(Long courseId:courseMap.keySet()){

                File file = new File("libs//lmsCourseUrlInfo//exam//"+courseId+"//"+courseMap.get(courseId)+".json");
                if(file.exists()&&file.length()>1){
                    continue;
                }
                newCourseMap.put(courseId,courseMap.get(courseId));
            }

            if(newCourseMap.size()>0){
                lmsWatchDTO.setCourseMap(newCourseMap);
                watchQueue.add(lmsWatchDTO);
            }
            // lmsCourseUrlInfo
        }

        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new GetCourseUrlTask("d:\\ynouAutoStudy\\chromedriver.exe", watchQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName("答题线程--" + i);
            answerThread.start();
        }


    }

}
